<?php


namespace App\Services;

class BaseService
{
    /**
     * 获取数据，数据库操作
     * @param $query
     * @param $param
     * @return mixed
     * @throws \Exception
     */
    public static function getData($query,$param,$func='')
    {
        try {
            if (isset($param['sorts'])) {
                foreach ($param['sorts'] as $key => $value) {
                    $query = $query->orderBy($key, $value);
                }
            }
            if (isset($param['like'])) {
                foreach ($param['like'] as $key => $value) {
                    $query = $query->where($key, 'like', $value);
                }
            }
            if (isset($param['time'])) {
                foreach ($param['time'] as $key => $value) {
                    $query = $query->where($key, '>=', $value[0])->where($key, '<=', $value[1]);
                }
            }
            if (isset($param['is'])) {
                foreach ($param['is'] as $key => $value) {
                    if (count($value) == 3) {
                        $query = $query->where($value[0], $value[1], $value[2]);
                    } elseif (count($value) == 2) {
                        $query = $query->where($value[0], '=', $value[1]);
                    }
                }
            }

            if (!empty($func)){
                $query = $func($query);
            }

            return $query->paginate($param['limit'] ?? 15)->toArray();

        }catch (\Exception $exception){
            throw $exception;
        }
    }
}